//
//  LoginViewController.m
//  PCNews
//
//  Created by PengchengWang on 16/3/7.
//  Copyright © 2016年 pengchengWang. All rights reserved.
//

#import "LoginViewController.h"
#import "SendValue.h"
#import "NewsViewController.h"
#import "RegistViewController.h"
#import <AVOSCloud.h>

@interface LoginViewController ()

@property(nonatomic,assign)Class myClass;

@property (weak, nonatomic) IBOutlet UITextField *txtUserName;

@property (weak, nonatomic) IBOutlet UITextField *txtPwd;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViews];
}

- (IBAction)btnLoginAction:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = @"正在登录中,请耐心等待......";
    [AVUser logInWithUsernameInBackground:self.txtUserName.text password:self.txtPwd.text block:^(AVUser *user, NSError *error) {
        hud.hidden = YES;
        if (user != nil) {
            UIAlertController *alterCon = [UIAlertController alertControllerWithTitle:@"恭喜您" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alterCon animated:YES completion:nil];
            //经过几秒后执行方法
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alterCon dismissViewControllerAnimated:YES completion:nil];
                [self leftBarAction];
            });
        } else {
            UIAlertController *alterCon = [UIAlertController alertControllerWithTitle:@"很遗憾" message:@"用户名密码有误" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alterCon animated:YES completion:nil];
            //经过几秒后执行方法
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alterCon dismissViewControllerAnimated:YES completion:nil];
            });
        }
    }];
}

- (IBAction)btnRegistAction:(id)sender {
    RegistViewController *registVC = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:registVC animated:YES];
}

-(void)setViews{
    UIBarButtonItem *leftbarBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"detial_buttom_back"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem = leftbarBtn;
    UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(260, 0, 100, 40)];
    lblTitle.text = @"欢迎登录 PC新闻";
    lblTitle.textColor = [UIColor cyanColor];
    self.navigationItem.titleView = lblTitle;
    UITapGestureRecognizer *tapRootView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapRootView)];
    [self.view addGestureRecognizer:tapRootView];
}

-(void)leftBarAction{
    self.myClass = [SendValue shareInstance].myclass;
    self.myClass = self.myClass?self.myClass:[NewsViewController class];
    [DrawerViewController setRootViewController:self.myClass];
}

-(void)tapRootView{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
